Python Charting Stocks Error

by: cssman, 9 years ago


Just finished video part 5 for charting stocks with python at: https://www.youtube.com/watch?v=OXaxZpgBMqc&list=PLQVvvaa0QuDcR-u9O8LyLR7URiKuW-XZq&index=5

When I run the module, I get the error: main loop global name 'SplitSource' is not defined for each stock symbol. Can someone help? Here's my code:

import urllib2
import time
import datetime

stocksToPull = 'AAPL','GOOG','MSFT','CMG','AMZN','EBAY','TSLA'

def pullData(stock):
    try:
        print 'Currently pulling',stock
        print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
        urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10d/csv'
        saveFileLine = stock+'.txt'

        try:
            readExistingData = open(saveFileLine,'r').read()
            splitExisting = readExistingData.split('n')
            mostRecentLine = splitExisiting[-2]
            lastUnix = mostRecentLine.split(',')[0]
        except:
            lastUnix = 0

        saveFile = open(saveFileLine,'a')
        sourceCode = urllib2.urlopen(urlToVisit).read()
        splitSource = sourceCode.split('n')

        for eachLine in SplitSource:
            if 'values' not in eachLine:
                splitLine = eachLine.split(',')
                if len(splitLine)==6:
                    if int(splitLine[0]) > int(lastUnix):
        
                        lineToWrite = eachLine+'n'
                        saveFile.write(lineToWrite)
        saveFile.close()

        print 'Pulled',stock
        print 'sleeping....'
        print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
        time.sleep(300)
                  
    except Exception,e:
        print 'main loop',str(e)

for eachStock in stocksToPull:
    pullData(eachStock)


***
Thanks



You must be logged in to post. Please login or register an account.



Precisely, re-look at your code:


        splitSource = sourceCode.split('n')

        for eachLine in SplitSource:


In one place, you call it splitSource, with a lowercase s to begin with, and the next you have a capital s. Choose one to go with.

-Harrison 9 years ago
Last edited 9 years ago

You must be logged in to post. Please login or register an account.


Aha! Thanks much Harrison, I'm really enjoying your tutorials. I seem to be finding them in bits and pieces on youtube. Is there a place where you have them all stored on pythonprogramming.net or some other url? Thanks again for your great tutorials.

-cssman 9 years ago

You must be logged in to post. Please login or register an account.